<template>
  <div class="app-container">
    <el-dialog
      :title="dialogtitle"
      :visible.sync="dialogVisible"
      width="98%"
      top="20px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
    >
      <Edit :infoid="id" :read-only="readOnly" :timestamp="timestamp" @dialogHide="dialogHide($event)" />
    </el-dialog>

    <div class="filter-container">
      <!-- <el-select v-model="listQuery.classCode" placeholder="请选择车辆类型" clearable style="margin-left: 10px;width: 200px" class="filter-item">
        <el-option label="全部" value="" />
        <el-option v-for="(it,index) in classList" :key="index" :label="it.label" :value="it.value" />
      </el-select> -->
      <el-input v-model="listQuery.applyNumber" placeholder="请输入申请编号" style="width: 200px;margin-left: 10px;" class="filter-item" />
      <el-input v-model="listQuery.productModel" placeholder="请输入规格型号" style="width: 200px;margin-left: 10px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter">
        搜索
      </el-button>

      <!-- <el-button v-if="roleEdit" style="margin-left: 10px;float:right" class="filter-item" type="primary" icon="el-icon-download" @click="showDic">
        类别管理
      </el-button> -->
      <!-- <el-button class="filter-item" style="margin-left: 10px;float:right" type="success" icon="el-icon-edit" @click="addnew">
        新建
      </el-button> -->
    </div>

    <el-table
      v-if="showTable"
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="50" align="center" /> -->

      <el-table-column align="center" prop="nid" label="编号" width="120" />
      <el-table-column align="center" prop="applynumber" label="申请编号" width="150" />
      <el-table-column width="120px" prop="cartype" label="车辆类型" align="center" />
      <el-table-column prop="companyname" label="车企名称" width="300" show-overflow-tooltip />
      <el-table-column min-width="300px" prop="productname" label="产品名称" show-overflow-tooltip />
      <el-table-column min-width="300px" prop="productmodel" label="产品型号" show-overflow-tooltip />

      <!-- <el-table-column label="状态" prop="user_enabled" width="100" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enabled" type="success" effect="dark" style="cursor:pointer" title="停用" @click="setState(scope.row.id,false,scope.row.realName)">正常</el-tag>
          <el-tag v-else type="info" effect="dark" style="cursor:pointer" title="启用" @click="setState(scope.row.id,true,scope.row.realName)">停用</el-tag>
        </template>
      </el-table-column> -->
      <!--
      <el-table-column label="状态" prop="state" width="90" align="center" sortable="custom">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state" type="success" effect="dark" style="cursor:pointer" title="取消发布此信息" @click="setState(scope.row.id,0,scope.row.title)">已发布</el-tag>
          <el-tag v-else type="info" effect="dark" style="cursor:pointer" title="发布此信息" @click="setState(scope.row.id,1,scope.row.title)">未发布</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="scope">

          <el-popconfirm title="确定生成报告吗？" style="margin-left:10px;" @onConfirm="lock(scope.row.applynumber,scope.row.productname)">
            <el-button slot="reference" type="primary" size="small" icon="el-icon-edit">
              生成报告
            </el-button>
          </el-popconfirm>
          <!-- <el-button v-if="selection" type="warning" size="small" icon="el-icon-success" style="margin-left:10px;" @click="selectInfo(scope.row)">
            选取
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> -->
  </div>
</template>

<script>
// import { fetchList } from '@/api/article'
// import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import Edit from './edit'

export default {
  name: 'ArticleList',
  components: { Edit },
  data() {
    return {
      showTable: false,
      roleEdit: false,
      rolePublisher: false,
      selection: false,
      dicListLoading: false,
      stopDicLoading: false,
      startDicLoading: false,
      submitLoading: false,
      classList: [],
      classForm: {
        parentClass: 14,
        label: ''
      },
      diclist: [],
      settingVisible: false,
      item: {},
      dialogtitle: '生成报告',
      dialogVisible: false,
      readOnly: false,
      timestamp: false,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        applyNumber: '2019051900010',
        productModel: 'BJ1088VEJEA-FA1'
      },
      Sortkey: '',
      Isace: '',
      multipleSelection: [],
      id: ''
    }
  },
  mounted() {
    if (JSON.parse(localStorage.getItem('user')).auths.includes('AUTH_close')) {
      this.roleEdit = true
    } else {
      this.roleEdit = false
    }
  },
  methods: {
    lock(applyNumber, productModel) {
      this.$http
        .post('/api/check/updateRequestState', { applyNumber: applyNumber, productModel: productModel, locked: true })
        .then(response => {
          if (response.data.success === true) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.id = applyNumber
            this.dialogVisible = true
            this.timestamp = new Date().getTime()
          } else {
            console.log('操作失败:' + response.data.message)
            this.$message({
              message: '操作失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
    },
    setState(id, state, title) {
      if (this.rolePublisher && !this.selection) {
        const stateText = state ? ' 发布 ' : ' 取消发布 '
        this.$confirm('确定要' + stateText + ' “' + title + '” 这信息吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http
            .post('/api/busInformation/save', { id: id, state: state })
            .then(response => {
              if (response.data.success === true) {
                this.$message({
                  message: stateText + '成功',
                  type: 'success'
                })
                this.getList()
              } else {
                console.log(stateText + '失败:' + response.data.message)
                this.$message({
                  message: stateText + '失败:' + response.data.message,
                  type: 'error'
                })
              }
            })
            .catch(error => {
              console.log(error)
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已放弃' + stateText + '操作'
          })
        })
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteData(id) {
      let ids = []
      if (id) {
        ids = [id]
      } else {
        ids = this.multipleSelection.map(item => item.id)
      }
      if (ids.length > 0) {
        this.$http
          .post('/api/knowledge/remove', { ids: ids })
          .then(response => {
            if (response.data.success === true) {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.getList()
            } else {
              console.log('删除失败:' + response.data.message)
              this.$message({
                message: '删除失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
          })
      } else {
        this.$message({
          message: '请至少选择一条信息',
          type: 'danger'
        })
      }
    },
    sortChange(column, prop, order) {
      this.Sortkey = column.prop
      if (column.order === 'ascending') {
        this.Isace = 'asc'
      } else {
        this.Isace = 'desc'
      }
      this.getList()
    },
    handleFilter() {
      // this.listQuery.page = 1
      this.getList()
    },
    addnew() {
      this.id = 0
      this.readOnly = false
      this.dialogtitle = '生成报告'
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    edit(id) {
      this.id = id
      this.readOnly = false
      this.dialogtitle = '编辑'
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    detail(id) {
      this.id = id
      this.readOnly = true
      this.dialogtitle = '查看'
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    dialogHide(flag) {
      this.dialogVisible = false
      this.getList()
    },
    getList() {
      this.listLoading = true
      this.$http
        .get('/api/check/queryCarReqList?applyNumber=' + this.listQuery.applyNumber + '&productModel=' + this.listQuery.productModel)
        .then(response => {
          this.listLoading = false
          if (response.data.success === true) {
            this.showTable = true
            this.list = [response.data.result]
            // this.total = response.data.result.total
            // if (response.data.result.current > response.data.result.pages && response.data.result.pages !== 0) {
            //   this.listQuery.page = response.data.result.pages
            //   this.getList()
            // }
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '未检索到匹配信息',
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.listLoading = false
        })
      // fetchList(this.listQuery).then(response => {
      //   this.list = response.data.items
      //   this.total = response.data.total
      //   this.listLoading = false
      // })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
